<?php

namespace App\V3\V3_2_7\Controllers;

use App\V3\V3_2_7\Models\AdminModel;
use App\V3\V3_2_7\Services\OrderService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

/**
 * 订单管理
 * AUTHOR  : zl
 * DateTime: 2022/08/18
 */
class OrderController extends BaseController
{
    /**
     * 微信支付
    */
    public function WechatPay(Request $request)
    { return $this->fail('', '购买会员或存储空间，请联系管理员！');
        //参数验证
        $validator = Validator::make($data = $request->all(), [
             'type' => [
                'required',
                Rule::In([1, 2]),
            ],//订单类型 1vip充值 2购买空间
            'pay_type' => [
                'required',
                Rule::In([1, 2]),
            ],//支付类型 1小程序 2app
        ],
        [
            'type.required' => '请选择类型',
            'type.in' => '类型不合法，请重新选择',
            'pay_type.required' => '请选择类型',
            'pay_type.in' => '类型不合法，请重新选择',

        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }
        $data['logTime'] = time();
        $data['main_id'] = $this->getMainUserId();
        $data['user_id'] = $this->getUserId();
        $this->operateLog('微信支付', $data, $data['logTime']);
        return response()->json(OrderService::createOrder($this->getMainUserId(),$data));
    }

    /**
     * 手动充值
     */
    public function handRecharge(Request $request)
    {
        $data = $request->all();
        //参数验证
//        $validator = Validator::make($data = $request->all(), [
//            'pay_money' => 'required',
//            'sel_type' => [
//                'required',
//                Rule::In([1, 2,3,4,5]),
//            ],//支付类型 1小程序 2app
//        ],
//            [
//                'pay_money.required' => '请选择充值金额',
//                'sel_type.required' => '请选择充值类型',
//                'sel_type.in' => '充值类型不合法，请重新选择',
//
//            ]);
        //$data['type']=1;
        $data['pay_type']=1;
        //参数错误响应
//        if ($info = $validator->errors()->first()) {
//            return $this->fail('', $info);
//        }
        $data['logTime'] = time();
        $data['main_id'] = $this->getMainUserId();
        $data['user_id'] = $this->getUserId();
        $this->operateLog('手动充值', $data, $data['logTime']);
        $accountId = AdminModel::query()->where([
            'username'=>$data['phone'],
            'delete_flag'=>0,
            'parent_id'=>0
        ])->value('id');
        if(empty($accountId)){
            return $this->fail('','没有找到账号');
        }
        return response()->json(OrderService::handCreateOrder($accountId,$data));
    }
}
